'''
Created on 2017年1月22日

@author: maliqiang
'''
import sys

#format str
print("name is %s,age %d" %('mark',26))

#基于字典的字符串格式化
print("%(a)d %(b)s" %{"a":2,"b":"test"})

#带有模板的格式化
#by position
template = 'hello!{0},{1} and {2}'
print(template.format( 'jerry','mark','jeven'))
#by keyword
temp = '{n1} and {n2}'
print(temp.format(n1='mark',n2='test'))

#complex
print('My {1[spam]} runs {0.platform}'.format(sys, {"spam":"laptop"}))


somelist=list('spam')
print('first ={0[0]},third ={0[2]}'.format(somelist))
print('first ={0},third ={1}'.format(somelist[0],somelist[2]))

#只有单个的正的偏移才起作用
ls = list('SPAM')
parts=ls[0],ls[-1],ls[1:3]
print('first={0},second={1},third={2}'.format(*parts))

#format num
print('{0:,d}'.format(999999))

#format num
print('{0:,.3f}'.format(999999,2567))